package myJs

import myJs.Implicits._
import myJs.Utils._
import myJs.myPkg._
import myJs.myPkg.jquery._
import scalatags.Text.all._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.scalajs.js.Dynamic.{global => g}
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}

/**
 * Created by yz on 2019/4/15
 */
@JSExportTopLevel("DetailInfo")
object DetailInfo {

  @JSExport("init")
  def init = {
    getPdfInfo
  }

  def getPdfInfo = {
    val url = g.jsRoutes.controllers.UserController.getPdfInfo().url.toString
    MyFetch.get(url = s"${url}",
      headers = Map("Content-Type" -> "application/json")).map { any =>
      refreshPdfInfo(any.toJDJAn)
    }.timeoutDeal

  }

  def refreshPdfInfo(dict: JDJAn) = {
    $("#title").text(dict.myGet("title"))
    $("#unit").text(dict.myGet("unit"))
    $("#address").text(dict.myGet("address"))
    $("#reporter").text(dict.myGet("reporter"))
    $("#checker").text(dict.myGet("checker"))
  }

  @JSExport("updateBefore")
  def updateBefore = {
    val url = g.jsRoutes.controllers.UserController.getPdfInfo().url.toString
    MyFetch.get(url = s"${url}?missionId=${id}",
      headers = Map("Content-Type" -> "application/json")).map { any =>
      refreshPdfInfoForm(any.toJDJAn)
    }.timeoutDeal
    $("#modal").modal("show")
  }

  def refreshPdfInfoForm(dict: JDJAn) = {
    $("#pdfForm input[name='title']").`val`(dict.myGet("title"))
    $("#pdfForm input[name='unit']").`val`(dict.myGet("unit"))
    $("#pdfForm input[name='address']").`val`(dict.myGet("address"))
    $("#pdfForm input[name='reporter']").`val`(dict.myGet("reporter"))
    $("#pdfForm input[name='checker']").`val`(dict.myGet("checker"))
  }

  @JSExport("updatePdfInfo")
  def updatePdfInfo = {
    val url = g.jsRoutes.controllers.UserController.updatePdfInfo().url.toString
    MyFetch.post(url = s"${url}", data = $("#pdfForm").serialize(),
      headers = Map("Content-Type" -> "application/x-www-form-urlencoded")).map { any =>
      getPdfInfo
      $("#modal").modal("hide")
      Swal.swal(SwalOptions.title("成功！").text("修改成功！").`type`("success"))
    }.timeoutDeal
  }

}
